package com.choosefine.springboot.devops.base.moudle.event;

import com.choosefine.springboot.devops.base.common.bean.CommonToolkitBean;
import com.choosefine.springboot.devops.base.common.bean.SimpleTemplateBean;
import com.choosefine.springboot.devops.base.common.component.InputMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
 * Created by 邱鹏 on 2016/11/26.
 */
public class ClickEvent implements EventCallback {
    Logger log = LoggerFactory.getLogger("微信/DefaultEvent");
    @Autowired
    Properties yaml;
    @Autowired
    CommonToolkitBean commonToolkitBean;

    @Override
    public Object execute(InputMessage inputMsg, Map tplDataMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String template = yaml.get("msg.return.passive.txt") + "";
        String content = "inputMsg.getEvent():" + inputMsg.getEvent() + ",inputMsg.getEventKey():" + inputMsg.getEventKey();
        if ("wechat_export_report".equals(inputMsg.getEventKey())) {
            template = yaml.get("msg.return.passive.media") + "";
            tplDataMap.put("media_id", "lLVSEAq6uVvLLMT98odksl9u5L9BrtOaYvH_ePWIrs8-mAGj0SFSwV1qXIpREK6x");
            tplDataMap.put("title", new String("大鹏恶搞视频".getBytes("utf-8"), "ISO8859_1"));
            tplDataMap.put("description", new String("里面有多位日本女优出演".getBytes("utf-8"), "ISO8859_1"));
        } else if ("wechat_interface_sys".equals(inputMsg.getEventKey())) {
            template = yaml.get("msg.return.passive.pic") + "";
            tplDataMap.put("media_id", "0LX5QvB92QKNC_9cluvg3418xMZXH6e05TzNGkKbYps9nEd0B8enPB8x2tt3wCvz");
        }
        String afterConvertStr = new String(content.getBytes("utf-8"), "ISO8859_1");//必须强制转码，否则无法显示
        tplDataMap.put("content", afterConvertStr);
        SimpleTemplateBean stb = commonToolkitBean.simpleTemplateBean;

        //将对象转换为XML字符串
        String xml = stb.template(template, tplDataMap, SimpleTemplateBean.TemplateType.DOLLAR);
        log.info("回复的消息：" + xml);
        //将内容发送给微信服务器，发送到用户手机
        response.getWriter().write(xml);
        return null;
    }
}
